package v3.chapter2.innerTest2;

import v3.chapter2.innerTest2.OutClass.InnerClass1;
import v3.chapter2.innerTest2.OutClass.InnerClass2;

public class Run {

	public static void main(String[] args) {
		final InnerClass1 in1=new InnerClass1();
		final InnerClass2 in2=new InnerClass2();
		Thread t1=new Thread(new Runnable() {
			public void run() {
				in1.method1(in2);
			}
		},"A");
		Thread t2=new Thread(new Runnable() {
			public void run() {
				in1.method2();
			}
		},"B");
		
		Thread t3=new Thread(new Runnable() {
			public void run() {
				in2.method1();
			}
		},"C");
		
		t1.start();
		t2.start();
		t3.start();
	}

}

class OutClass {
	static class InnerClass1 {
		public void method1(InnerClass2 class2) {
			String threadName=Thread.currentThread().getName();
			synchronized (class2) {
				System.out.println(threadName+" 进入 InnerClass1 类中的method1方法");
				for (int i = 1; i <= 10; i++) {
					System.out.println(Thread.currentThread().getName() + " i=" + i);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.println(threadName+" 离开 InnerClass1 类中的method1方法");
			}
		}

		synchronized public void method2() {
			String threadName=Thread.currentThread().getName();
			System.out.println(threadName+" 进入 InnerClass1 类中的method2方法");
			for (int i = 11; i <= 20; i++) {
				System.out.println(Thread.currentThread().getName() + " i=" + i);
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(threadName+" 离开 InnerClass1 类中的method2方法");
		}
	}
	
	static class InnerClass2{
		synchronized public void method1() {
			String threadName=Thread.currentThread().getName();
			System.out.println(threadName+" 进入 InnerClass2 类中的method1方法");
			for (int i = 11; i <= 20; i++) {
				System.out.println(Thread.currentThread().getName() + " i=" + i);
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println(threadName+" 离开 InnerClass2 类中的method1方法");
		}
	}
}